package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlApprentice;
import com.ruoyi.sl.service.ISlGprocessService;
import com.ruoyi.sl.service.ISlModuleService;
import com.ruoyi.sl.service.ISlWorkshopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlProcess;
import com.ruoyi.sl.service.ISlProcessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工序Controller
 * 
 * @author admin
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/sl/process")
public class SlProcessController extends BaseController
{
    @Autowired
    private ISlProcessService slProcessService;

    @Autowired
    private ISlModuleService slModuleService;

    @Autowired
    private ISlWorkshopService slWorkshopService;

    @Autowired
    private ISlGprocessService slGprocessService;

    /**
     * 查询工序列表
     */
    @PreAuthorize("@ss.hasPermi('sl:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlProcess slProcess)
    {
        startPage();
        List<SlProcess> list = slProcessService.selectSlProcessList(slProcess);
        return getDataTable(list);
    }

    /**
     * 根据大工序ID查询所有小工序
     */
    @GetMapping(value = "/plist/{gprocessId}")
    public AjaxResult getProcesslistByGprocessId(@PathVariable Long gprocessId)
    {
        return success(slProcessService.selectSlProcessListByGprocessId(gprocessId));
    }

    /**
     * 导出工序列表
     */
    @PreAuthorize("@ss.hasPermi('sl:process:export')")
    @Log(title = "工序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlProcess slProcess)
    {
        List<SlProcess> list = slProcessService.selectSlProcessList(slProcess);
        ExcelUtil<SlProcess> util = new ExcelUtil<SlProcess>(SlProcess.class);
        util.exportExcel(response, list, "工序数据");
    }

    /**
     * 获取工序详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:process:query')")
    @GetMapping(value = { "/", "/{processId}" })
    public AjaxResult getInfo(@PathVariable(value = "processId", required = false) Long processId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        if (StringUtils.isNotNull(processId))
        {
            SlProcess slProcess = slProcessService.selectSlProcessByProcessId(processId);
            ajax.put(AjaxResult.DATA_TAG, slProcess);
            //获取模块对应的车间
            if(StringUtils.isNotNull(slProcess.getModuleId())){
                ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(slProcess.getModuleId()));
            }
            //获取车间对应的大工序
            if(StringUtils.isNotNull(slProcess.getWorkshopId())){
                ajax.put("gprocesss", slGprocessService.selectSlGprocessListByWorkshopId(slProcess.getWorkshopId()));
            }
        }
        return ajax;
    }

    /**
     * 新增工序
     */
    @PreAuthorize("@ss.hasPermi('sl:process:add')")
    @Log(title = "工序", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlProcess slProcess)
    {
        if (!slProcessService.checkProcessName(slProcess))
        {
            return error("添加失败,工序名'" + slProcess.getProcessName() + "'已存在");
        }
        slProcess.setCreateBy(getUsername());
        return toAjax(slProcessService.insertSlProcess(slProcess));
    }

    /**
     * 修改工序
     */
    @PreAuthorize("@ss.hasPermi('sl:process:edit')")
    @Log(title = "工序", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlProcess slProcess)
    {
        if (!slProcessService.checkProcessName(slProcess))
        {
            return error("修改失败,工序名'" + slProcess.getProcessName() + "'已存在");
        }
        slProcess.setUpdateBy(getUsername());
        return toAjax(slProcessService.updateSlProcess(slProcess));
    }

    /**
     * 删除工序
     */
    @PreAuthorize("@ss.hasPermi('sl:process:remove')")
    @Log(title = "工序", businessType = BusinessType.DELETE)
	@DeleteMapping("/{processIds}")
    public AjaxResult remove(@PathVariable Long[] processIds)
    {
        return toAjax(slProcessService.deleteSlProcessByProcessIds(processIds));
    }
}
